home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / PixMaps 1.1 / ReadMe < prev   
Encoding:
Text File  |  1997-07-12  |  17.0 KB  |  535 lines  |  [ttro/ttxt]

  1. PixMaps 1.1
  2. PixMap manipulation routines, ReadMe.
  3. Copyright (c) 1996, 1997 by John Montbriand.  All Rights Reserved.
  4. Permission hereby granted for public use.
  5. Distribute freely in areas where the laws of copyright apply.
  6. USE AT YOUR OWN RISK.
  7. DO NOT DISTRIBUTE MODIFIED COPIES.
  8. Comments/questions/postcards* to the author at the address:
  9.   John Montbriand
  10.   P.O. Box. 1133
  11.   Saskatoon Saskatchewan Canada
  12.   S7K 3N2
  13. or by email at:
  14.    tinyjohn@sk.sympatico.ca
  15. *if you mail a postcard, then I will provide you with technical support
  16. regarding questions you may have about this file.
  17.  
  18. 1.1 Changes: 
  19. Added documentation for the PixMap2PixPat routine.
  20. Simplified GDevice creation code.
  21.  
  22. Contents
  23. About PixMaps...
  24. Implementation Notes
  25. Materials
  26. Creating and Disposing of PixMaps
  27.     MakePixMap
  28.     Make16BitPixMap and Make32BitPixMap
  29.     MakeScreenLikePixMap
  30.     KillPixMap
  31. PixMap Information Access
  32.     PixMapSize 
  33.     SetPixMapPixel and GetPixMapPixel
  34.     CalcPixMapColours
  35. Flips and Rotates for PixMaps
  36.     RotatePixRight
  37.     RotatePixLeft
  38.     FlipPixVertical
  39.     FlipPixHorizontal
  40.     RotatePixMap
  41.     DuplicatePixMap
  42. Converting Pictures to PixMaps
  43.     PICTToPixMap, PICTTo16BitPixMap, and PICTTo32BitPixMap
  44. Converting PixMaps to Pictures
  45.     PixMapToPICT
  46.     PixMapToCompressedPICT
  47. Converting PixMaps to Pixel Patterns
  48.     PixMap2PixPat
  49. A Macro for Drawing Into PixMaps
  50.     WithPixMap
  51. Drawing and Plotting Routines
  52.     PlotPixMap
  53.     PixMapCopy
  54. Files in this package
  55. Copies for sale!
  56. NO WARRANTY
  57. Bug reports make this a better product!
  58. Further Reference
  59. Other
  60.  
  61. About PixMaps...
  62.     
  63. PixMaps define a data type used for the storage, retrieval, and display of 1, 2, 4, 8,16, and 32 bit raster data on Mac OS compatible computers.  QuickDraw drawing routines can be used to draw directly into PixMaps, and PixMaps can be used for both black and white and color images.  PixMaps allow you to do color off-screen drawing. Drawing things off screen and then placing them on screen creates the feel of instantaneous, flicker free drawing.  
  64.     
  65. QuickDraw CGrafPorts draw into PixMap structures, and, as a matter of fact, all screen drawing on newer Mac OS compatible computers is implemented through PixMaps that are memory mapped to the screen hardware.  PixMaps are an extension of the BitMap, and can be, for most purposes, used interchangeably.
  66.     
  67. Routines and methods used herein are similar to the corresponding routines found in the BitMaps package.
  68.  
  69. Implementation Notes
  70.  
  71. KillPixMap  can be called from GrowZone functions.
  72.  
  73. The implementation for many of the routines herein 'special cases' some operations for different PixMap depths (i.e. 1, 2, 4, 8, 16, and 32 bit ones) for performance reasons.
  74.  
  75. Materials
  76.  
  77. The PixMap definition is the copyright property of Apple Computer and has been known to the public since 1986.
  78.     
  79. The routines documented herein are the copyright property of John Montbriand.  I am making these routines available to the Mac OS programming community because I feel it's about time someone did!  They're fun, easy to use, and practical.  I hope all who find them will use them well.
  80.  
  81.  
  82. Creating and Disposing of PixMaps
  83.  
  84. PixMaps can be created for a specific depth or as the closest match for a specific area on the screen.  Routines for creating PixMaps are as follows:
  85.  
  86. MakePixMap
  87.  
  88. PixMapHandle MakePixMap(short width, short height, CTabHandle clut);
  89.  
  90. MakePixMap creates a new PixMap handle with the requested dimensions.  If there is not enough memory to allocate the PixMap, MakePixMap will return NULL.  if clut is NULL, then the default 256 color table (clut = 8) is used.  if clut is not NULL, then a copy of it is used in the PixMap.  The number of colors in the clut determines the pixel depth of the created PixMap as follows:
  91.         2 colors -> 1 pit per pixel,
  92.         3 to 4 colors -> 2 bits per pixel,
  93.         5 to 16 colors -> 4 bits per pixel,
  94.         17 to 256 colors -> 8 bits per pixel.
  95. EXAMPLE:  
  96.  
  97. PixMapHandle pix;
  98.  
  99. pix = MakePixMap(100, 100, NULL);
  100. if (pix == NULL) { err = memFullErr; goto bail; }
  101.  
  102.  
  103. Make16BitPixMap and Make32BitPixMap
  104.  
  105. PixMapHandle Make16BitPixMap(short width, short height);
  106. PixMapHandle Make32BitPixMap(short width, short height);
  107.  
  108. Make16BitPixMap creates a new PixMap handle with the requested dimensions.  If there is not enough memory to allocate the PixMap, Make16BitPixMap will return NULL.  The created PixMap utilizes 16 bit color and allocates 16 bits per each pixel.  Make32BitPixMap is identical however it allocates 32 bits per pixel.
  109.  
  110. EXAMPLE:  
  111.  
  112. PixMapHandle pix;
  113.  
  114. pix = Make16BitPixMap(100, 100, NULL);
  115. if (pix == NULL) { err = memFullErr; goto bail; }
  116.  
  117.  
  118. MakeScreenLikePixMap
  119.  
  120. PixMapHandle MakeScreenLikePixMap(Rect *globalRect);
  121.  
  122. MakeScreenLikePixMap creates a PixMap mirroring the attributes of the PixMap used to draw largest area of *globalRect on the screen.
  123.  
  124. EXAMPLE:  
  125.  
  126. PixMapHandle pix;
  127. Rect r;
  128. WindowPtr w;
  129.  
  130. SetPort(w);
  131. r = w->portRect;
  132. LocalToGlobal((Point*) &r.top);
  133. LocalToGlobal((Point*) &r.bottom);
  134. pix = MakeScreenLikePixMap(&r);
  135. if (pix == NULL) { err = memFullErr; goto bail; }
  136.  
  137.  
  138. KillPixMap
  139.  
  140. void KillPixMap(PixMapHandle pix);
  141.  
  142. KillPixMap disposes of a PixMap allocated by one of the routines herein.
  143.  
  144. EXAMPLE:  
  145.  
  146. PixMapHandle pix;
  147.  
  148. pix = Make16BitPixMap(100, 100, NULL);
  149. if (pix == NULL) { err = memFullErr; goto bail; }
  150. ....
  151. KillPixMap(pix);
  152.  
  153.  
  154. PixMap Information Access
  155.  
  156. PixMapSize 
  157.  
  158. long PixMapSize(PixMapHandle pix);
  159.  
  160. PixMapSize returns the number of bytes occupied by the PixMap, its color table, and its raster data.
  161.  
  162. EXAMPLE:  
  163.  
  164. PixMapHandle pix;
  165. long bytecount;
  166.  
  167. pix = Make16BitPixMap(100, 100, NULL);
  168. if (pix == NULL) { err = memFullErr; goto bail; }
  169. bytecount = PixMapSize(pix);
  170.  
  171.  
  172. SetPixMapPixel and GetPixMapPixel
  173.  
  174. void SetPixMapPixel(PixMapHandle pix, short h, short v, long value);
  175. long GetPixMapPixel(PixMapHandle pix, short h, short v);
  176.  
  177. SetPixMapPixel and GetPixMapPixel are for getting or setting individual pixel values.
  178.  
  179. EXAMPLE:  
  180.  
  181. PixMapHandle pix;
  182. long pixelvalue;
  183.  
  184. pix = Make16BitPixMap(100, 100, NULL);
  185. if (pix == NULL) { err = memFullErr; goto bail; }
  186. pixelvalue = GetPixMapPixel(pix, 25, 32);
  187.  
  188.  
  189. CalcPixMapColours
  190.  
  191. CTabHandle CalcPixMapColours(PixMapHandle pix);
  192.  
  193. CalcPixMapColours calculates a color table for the colors used in the PixMap.  For indexed images, this routine returns a copy of the PixMap's color table, otherwise, for direct images, it calls GetPixMapInfo.
  194.  
  195. EXAMPLE:  
  196.  
  197. PixMapHandle pix;
  198. CTabHandle theclut;
  199.  
  200. pix = Make16BitPixMap(100, 100, NULL);
  201. if (pix == NULL) { err = memFullErr; goto bail; }
  202. theclut = CalcPixMapColours(pix);
  203. if (theclut == NULL) { err = memFullErr; goto bail; }
  204.  
  205.  
  206. Flips and Rotates for PixMaps
  207.  
  208. RotatePixRight
  209.  
  210. PixMapHandle RotatePixRight(PixMapHandle pix);
  211.  
  212. RotatePixRight creates a new PixMap containing the image stored in the parameter PixMap rotated 90 degrees to the right. The resulting PixMap is appropriately sized:  i.e. if the source PixMap is 100 pixels wide and 200 pixels tall,  then the result PixMap pointer will be 200 pixels wide and 100 pixels tall.  If an error occurs, RotatePixRight returns NULL.
  213.  
  214. EXAMPLE:  
  215.  
  216. PixMapHandle pix, nextpix;
  217.  
  218. pix = Make16BitPixMap(100, 100, NULL);
  219. if (pix == NULL) { err = memFullErr; goto bail; }
  220. nextpix = RotatePixRight(pix);
  221. if (nextpix == NULL) { err = memFullErr; goto bail; }
  222.  
  223.  
  224. RotatePixLeft
  225.  
  226. PixMapHandle RotatePixLeft(PixMapHandle pix);
  227.  
  228. RotatePixLeft creates a new PixMap containing the image stored in the parameter bitmap pointer rotated 90 degrees to the left.  The resulting PixMap is appropriately sized:  i.e. if the source PixMap is 100 pixels wide and 200 pixels tall,  then the result PixMap pointer will be 200 pixels wide and 100 pixels tall. If an error occurs, RotatePixLeft returns NULL.
  229.  
  230. EXAMPLE:  
  231.  
  232. PixMapHandle pix, nextpix;
  233.  
  234. pix = Make16BitPixMap(100, 100, NULL);
  235. if (pix == NULL) { err = memFullErr; goto bail; }
  236. nextpix = RotatePixLeft(pix);
  237. if (nextpix == NULL) { err = memFullErr; goto bail; }
  238.  
  239.  
  240. FlipPixVertical
  241.  
  242. PixMapHandle FlipPixVertical(PixMapHandle pix);
  243.  
  244. FlipPixVertical creates a new PixMap containing the image stored in the parameter PixMap flipped upside down.  The resulting PixMap will be the same size as the original image.    
  245.  
  246. EXAMPLE:  
  247.  
  248. PixMapHandle pix, nextpix;
  249.  
  250. pix = Make16BitPixMap(100, 100, NULL);
  251. if (pix == NULL) { err = memFullErr; goto bail; }
  252. nextpix = FlipPixVertical(pix);
  253. if (nextpix == NULL) { err = memFullErr; goto bail; }
  254.  
  255.  
  256. FlipPixHorizontal
  257.     
  258. PixMapHandle FlipPixHorizontal(PixMapHandle pix);
  259.  
  260. FlipPixHorizontal creates a new PixMap containing the image stored in the parameter PixMap flipped horizontally. The resulting PixMap will be the same size as the original image.
  261.  
  262. EXAMPLE:  
  263.  
  264. PixMapHandle pix, nextpix;
  265.  
  266. pix = Make16BitPixMap(100, 100, NULL);
  267. if (pix == NULL) { err = memFullErr; goto bail; }
  268. nextpix = FlipPixHorizontal(pix);
  269. if (nextpix == NULL) { err = memFullErr; goto bail; }
  270.  
  271.  
  272. RotatePixMap
  273.     
  274. PixMapHandle RotatePixMap(PixMapHandle pix, short cx, short cy, float angle);
  275.  
  276. RotatePixMap creates a new PixMap containing the image from the parameter PixMap rotated angle degrees about the center (cx, cy).  The resultant PixMap will have the same dimensions as the parameter PixMap regardless of the angle specified.
  277.  
  278. EXAMPLE:  
  279.  
  280. PixMapHandle pix, nextpix;
  281.  
  282. pix = Make16BitPixMap(100, 100, NULL);
  283. if (pix == NULL) { err = memFullErr; goto bail; }
  284. nextpix = RotatePixMap(pix, 50, 50, 53.7);
  285. if (nextpix == NULL) { err = memFullErr; goto bail; }
  286.  
  287.  
  288. DuplicatePixMap
  289.  
  290. PixMapHandle DuplicatePixMap(PixMapHandle pix);
  291.  
  292. DuplicatePixMap creates a copy of the PixMap parameter.
  293.  
  294. EXAMPLE:  
  295.  
  296. PixMapHandle pix, nextpix;
  297.  
  298. pix = Make16BitPixMap(100, 100, NULL);
  299. if (pix == NULL) { err = memFullErr; goto bail; }
  300. nextpix = DuplicatePixMap(pix, 50, 50, 53.7);
  301. if (nextpix == NULL) { err = memFullErr; goto bail; }
  302.  
  303.  
  304. Converting Pictures to PixMaps
  305.  
  306. PICTToPixMap, PICTTo16BitPixMap, and PICTTo32BitPixMap
  307.  
  308. PixMapHandle PICTToPixMap(PicHandle pic, CTabHandle clut);
  309. PixMapHandle PICTTo16BitPixMap(PicHandle pic);
  310. PixMapHandle PICTTo32BitPixMap(PicHandle pic);
  311.  
  312. PICTToPixMap, PICTTo16BitPixMap, or PICTTo32BitPixMap create a new PixMap the using the size information provided in the QuickDraw picture pointer parameter and draws the picture in the PixMap before returning the PixMap.
  313.  
  314. EXAMPLE:  
  315.  
  316. PixMapHandle pix;
  317. PicHandle pic;
  318.  
  319. pic = GetPicture(128);
  320. if (pic == NULL) {
  321.     err = ResError();
  322.     if (err == noErr) err = resNotFound;
  323.     goto bail;
  324. }
  325. pix = PICTTo16BitPixMap(pic);
  326. if (pix == NULL) { err = memFullErr; goto bail; }
  327.  
  328.  
  329. Converting PixMaps to Pictures
  330.  
  331. PixMapToPICT
  332.  
  333. PicHandle PixMapToPICT(PixMapHandle pix);
  334.  
  335. PixMapToPICT returns a QuickDraw picture that will draw the image stored in the PixMap.  PixMapToPICT is the inverse of PICTToPixMap.
  336.  
  337. EXAMPLE:  
  338.  
  339. PixMapHandle pix;
  340. PicHandle pic;
  341.  
  342. pix = Make16BitPixMap(100, 100);
  343. if (pix == NULL) { err = memFullErr; goto bail; }
  344. ...
  345. pic = PixMapToPICT(pix);
  346. if (pic == NULL) { err = memFullErr; goto bail; }
  347.  
  348.  
  349. PixMapToCompressedPICT
  350.  
  351. PicHandle PixMapToCompressedPICT(PixMapHandle pix);
  352.  
  353. PixMapToCompressedPICT returns a compressed QuickDraw picture that will draw the image stored in the PixMap.  PixMapToCompressedPICT uses the QuickTime 'jpeg' compressor.  If QuickTime is not installed or if an error occurs, this routine returns NULL.
  354.  
  355. EXAMPLE:  
  356.  
  357. PixMapHandle pix;
  358. PicHandle pic;
  359.  
  360. pix = Make16BitPixMap(100, 100);
  361. if (pix == NULL) { err = memFullErr; goto bail; }
  362. ...
  363. pic = PixMapToCompressedPICT(pix);
  364. if (pic == NULL) { err = memFullErr; goto bail; }
  365.  
  366.  
  367. Converting PixMaps to Pixel Patterns
  368.  
  369. PixMap2PixPat
  370.  
  371. PixPatHandle PixMap2PixPat(PixMapHandle pix, Pattern *the_pattern);
  372.  
  373. PixMap2PixPat converts a pixmap to a pixel pattern resource.  The pixmap's dimensions should be some power of two (i.e. 8, 16, 32, 64, or 128).  If an error occurs, NULL is returned.
  374.  
  375. EXAMPLE:  
  376.  
  377. PixMapHandle pix;
  378. PixPatHandle ppat;
  379.  
  380. pix = MakePixMap(128, 128, NULL);
  381. if (pix == NULL) { err = memFullErr; goto bail; }
  382. ...
  383. ppat = PixMap2PixPat(pix);
  384. if (ppat == NULL) { err = memFullErr; goto bail; }
  385.  
  386.  
  387. A Macro for Drawing Into PixMaps
  388.  
  389. WithPixMap
  390.  
  391. WithPixMap(pix, pxmp) { statement... }
  392.  
  393. WithPixMap is a macro facility that sets up the drawing environment such that any drawing commands in the statement following the macro instantiation will draw into the PixMap handle provided as the first parameter.  pix is a PixMapHandle,  pxmp is a variable of type PixMapPort*.  You must define these variables yourself.  For example,
  394.  
  395. PixMapHandle pix;
  396. PixMapPort* pxmp;
  397.  
  398. EXAMPLE:  
  399.  
  400. PixMapHandle pix;
  401. PixMapPort* pxmp;
  402.  
  403. pix = Make16BitPixMap(100, 100);
  404. if (pix == NULL) { err = memFullErr; goto bail; }
  405. WithPixMap(pix, pxmp) {
  406.     MoveTo(30, 30);
  407.     DrawString("\pHello World");
  408. }
  409. ...
  410.  
  411.  
  412.  
  413. Drawing and Plotting Routines
  414.  
  415. PlotPixMap
  416.  
  417. void PlotPixMap(PixMapHandle pix, short h, short v, short mode);
  418.  
  419. PlotPixMap provides a simple interface for drawing a PixMap in the current GrafPort.  The PixMap is drawn with the top left corner aligned with the point (h,v) using the indicated transfer mode.
  420.  
  421. EXAMPLE:  
  422.  
  423. PixMapHandle pix;
  424. PixMapPort* pxmp;
  425.  
  426. pix = Make16BitPixMap(100, 100);
  427. if (pix == NULL) { err = memFullErr; goto bail; }
  428. WithPixMap(pix, pxmp) {
  429.     MoveTo(30, 30);
  430.     DrawString("\pHello World");
  431. }
  432. PlotPixMap(pix, 20, 34, srcCopy);
  433. ...
  434.  
  435.  
  436. PixMapCopy
  437.  
  438. void PixMapCopy(PixMapHandle pix, Rect *src, Rect *dst, short mode);
  439.  
  440. PixMapCopy copies bits from the PixMap to the current port from the src rectangle to the destination using the indicated copy mode.
  441.  
  442. EXAMPLE:  
  443.  
  444. PixMapHandle pix;
  445. PixMapPort* pxmp;
  446. Rect src;
  447. WindowPtr w;
  448.  
  449. SetRect(src, 0, 0, 100, 100);
  450. pix = Make16BitPixMap(100, 100);
  451. if (pix == NULL) { err = memFullErr; goto bail; }
  452. WithPixMap(pix, pxmp) {
  453.     MoveTo(30, 30);
  454.     DrawString("\pHello World");
  455. }
  456. PixMapCopy(pix, &src, &w->portRect, srcCopy);
  457. ...
  458.  
  459.  
  460.  
  461. Files in this package
  462.  
  463. PixMap.c -- source code for the PixMap routines' implementation
  464. PixMap.h -- header file providing interfaces to the PixMap routines
  465. ReadMe  -- this file
  466.  
  467. :example: -- an Example
  468. :obj: -- object files made during compile
  469. PixMap.c -- the PixMap library file
  470. PixMap.h -- the PixMap library interface file
  471. CSApp.c  -- callback skeleton application, implementation
  472. CSApp.h  -- callback skeleton application, interface
  473. PixExample.rsrc -- resource file for the example
  474. PixExample.c -- implementation file for the example
  475. PixExample.r -- rez file for the example
  476. PixExample -- a compiled example
  477. MakeFile -- make command file, builds the example
  478.  
  479. :Libraries: -- precompiled libraries, ready to link.  
  480. PixMap.o -- 68K version of the PixMap library
  481. PixMap.xcoff -- PowerPC version of the PixMap library
  482.  
  483.  
  484. Copies for sale!
  485.  
  486. These libraries are provided for free and you may use them in any program you make;  however, if you would like to purchase a copy of these libraries and have a legal paper trail establishing your right to use them, then send along a cheque or a money order in the amount of $25.00 for the purchase of one copy.  I'll send you a receipt.
  487.  
  488.  
  489. NO WARRANTY
  490.  
  491. No warranties are made regarding these files.  John Montbriand disclaims all warranties regarding these files, either express or implied, including but not limited to implied warranties of merchantability and fitness for any particular purpose.  These files are provided "AS IS" without any warranty of any kind.  Use them at your own risk.  Copies of these files are not for sale in areas where the law does not allow exclusion of implied warranties.
  492.  
  493.  
  494. Bug reports make this a better product!
  495.  
  496. As in all of my products, I advertise a $10.00 finder's fee for bug reports that lead to corrections.  If you find a problem here, report it!  it could be worth your while....
  497.  
  498.  
  499. Further Reference
  500.  
  501. PixMap.h and PixMap.c contain routines for accessing and manipulating PixMap data stored in PixMapHandles.  For more information about PixMaps, offscreen drawing, Handles, C, etc..  refer to the following sources:
  502.     
  503. "Pixel Maps" (4-9) in the "Color QuickDraw" chapter of Inside Macintosh: Imaging With QuickDraw by Apple Computer, Inc.  Addison-Wesley
  504. A discussion of the PixMap data structure and how it applies to CGrafPorts.
  505.         
  506. "Copying Pixels Between Color Graphics Ports" (4-26) in the "Color QuickDraw" chapter of Inside Macintosh: Imaging With QuickDraw by Apple Computer, Inc.  Addison-Wesley.
  507. A discussion of the Bitmap data structure and how it applies to CGrafPorts.
  508.  
  509. "Boolean Transfer Modes With Color Pixels" (4-32) in the "Color QuickDraw" chapter of Inside Macintosh: Imaging With QuickDraw by Apple Computer, Inc.  Addison-Wesley.
  510. Some discussion of the various transfer modes and how they apply to PixMaps.
  511.  
  512. Both the "Graphics Devices" chapter and the "Offscreen Graphics Worlds" chapter of Inside Macintosh: Imaging With QuickDraw by Apple Computer, Inc.  Addison-Wesley.
  513. Other information related to PixMaps.
  514.     
  515. The "Image Compression Manager" chapter of Inside Macintosh: QuickTime by Apple Computer, Inc.  Addison-Wesley.
  516. Information related to using PixMaps with the image compression facilities provided by QuickTime.
  517.     
  518. Technical note QD13 "Principia Off-Screen Graphics Environments" by Forrest Tanaka.  Apple Computer, Inc.
  519. A discussion of offscreen drawing with PixMaps.
  520.  
  521. Inside Macintosh: Memory by Apple Computer, Inc.  Addison-Wesley.
  522. A discussion of memory management, handles, what they are, and  how to use them.
  523.     
  524. The C Programming Language 2nd edition by Brian W. Kernighan and Dennis M. Ritchie.  Prentice Hall.
  525.  
  526.  
  527. Other
  528.  
  529. Have a great day!
  530.  
  531. John Montbriand
  532. Author
  533.  
  534.  
  535.